package com.xmread.cn.network;

import com.google.gson.Gson;
import com.xmread.cn.bean.ErrorResponse;
import java.io.IOException;
import java.lang.reflect.Type;
import okhttp3.ResponseBody;
import retrofit2.Converter;

public class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Gson gson;
    private final Type type;

    public GsonResponseBodyConverter(Gson gson2, Type type2) {
        this.gson = gson2;
        this.type = type2;
    }

    public T convert(ResponseBody responseBody) throws IOException {
        String string = responseBody.string();
        ErrorResponse errorResponse = (ErrorResponse) this.gson.fromJson(string, ErrorResponse.class);
        if (errorResponse.code == 1401) {
//            BookCacheUtil.loginOut();
        }
        if (errorResponse.code == 200 || errorResponse.code == 1) {
            return this.gson.fromJson(string, this.type);
        }
        throw new ServerException(((ErrorResponse) this.gson.fromJson(string, ErrorResponse.class)).msg);
    }
}
